package com.zhrenjie04.alex.manager;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.zhrenjie04.alex.core.Permission;

/**
 * @author 张人杰
 */
@Import({ WebConfig.class, ContextConfig.class, SwaggerConfig.class })
@ImportResource({ "classpath:captcha.xml" })
@EnableTransactionManagement
@SpringBootApplication
@EnableDiscoveryClient
@EnableCaching
public class EpManagerApplication {

	private static final Logger logger = Logger.getLogger(EpManagerApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(EpManagerApplication.class, args);
	}

	@Value("${springCacheExpiration}")
	private long springCacheExpiration;

	@Autowired
	private RequestMappingHandlerMapping requestMappingHandlerMapping;

	@Bean
	public Object settingBean1(CacheManager cacheManager) {
		logger.info(">>>>>>>>>>" + cacheManager.getClass().getName());
		RedisCacheManager manager = (RedisCacheManager) cacheManager;
		manager.setDefaultExpiration(springCacheExpiration);
		return new Object();
	}

	@Bean
	public Object testBean(PlatformTransactionManager platformTransactionManager) {
		System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
		return new Object();
	}

	@Bean
	public Object testBean2(RedisTemplate redisTemplate) {
		logger.info(">>>>>>>>>>" + redisTemplate.getClass().getName());
		return new Object();
	}

	@PostConstruct
	private void printControllerUrls() {
		logger.info("####################################################################");
		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
		for (RequestMappingInfo info : map.keySet()) {
			Permission permission = map.get(info).getBeanType().getAnnotation(Permission.class);
			Permission methodPermission = map.get(info).getMethodAnnotation(Permission.class);
			logger.info((permission == null ? null : permission.value()) + "."
					+ (methodPermission == null ? null : methodPermission.value()) + ","
					+ info.getPatternsCondition().toString() + "," + info.getMethodsCondition() + ","
					+ map.get(info).getBean().toString() + ",["
					+ (permission == null ? "controller_no_permission" : "controller_with_permission") + ":"
					+ (permission == null ? null : permission.value()) + "]" + ",["
					+ (methodPermission == null ? "method_no_permission" : "method_with_permission") + ":"
					+ (methodPermission == null ? null : methodPermission.value()) + "]");
		}
		logger.info("#####################################################################");
	}
}
